package com.baoyouqun.controller.weixin;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baoyouqun.domain.VO.PostTypeDTO;
import com.baoyouqun.domain.cmd.SetUserFollowPostTypesCmd;
import com.baoyouqun.service.UserFollowPostTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wx/user/follow")
@Api(tags = "用户关注帖子类型接口")
public class WxUserFollowController {

    @Resource
    private UserFollowPostTypeService userFollowPostTypeService;

    @SaIgnore
    @GetMapping("/getUserFollowPostTypes")
    @ApiOperation("获取用户关注的帖子类型列表（单层）")
    public SingleResponse<List<PostTypeDTO.PostTypeSonDTO>> getUserFollowPostTypes() {
        if (!StpUtil.isLogin()) {
            List<PostTypeDTO.PostTypeSonDTO> defaultList = new ArrayList<>();
            String[] defaultTitles = {"资源", "市场", "资金", "人脉"};

            for (String title : defaultTitles) {
                PostTypeDTO.PostTypeSonDTO defaultSonDTO = new PostTypeDTO.PostTypeSonDTO();
                defaultSonDTO.setTitle(title);
                defaultSonDTO.setCount(0); // 默认数量设为0（可根据业务调整）
                // 子列表默认是空ArrayList，无需额外设置
                defaultList.add(defaultSonDTO);
            }
            return SingleResponse.of(defaultList);
        }

        String userId = StpUtil.getLoginIdAsString();
        List<PostTypeDTO.PostTypeSonDTO> followTypes = userFollowPostTypeService.getUserFollowPostTypes(userId);
        return SingleResponse.of(followTypes);
    }

    
    @PostMapping("/setUserFollowPostTypes")
    @ApiOperation("设置用户关注的帖子类型列表（单层）")
    public Response setUserFollowPostTypes(@RequestBody SetUserFollowPostTypesCmd cmd) {
        String userId = StpUtil.getLoginIdAsString();
        userFollowPostTypeService.setUserFollowPostTypes(userId, cmd.getFollowTitles());
        return Response.buildSuccess();
    }
}